﻿using System;



namespace BlamanticUI.Components;

/// <summary>
/// Render a configured toast message.
/// </summary>
internal class ToastBox : BlazorComponentBase, IDisposable
{

    public ToastSetting Setting => Controller.Setting;

    [Parameter] public ToastController Controller { get; set; }

    RenderFragment Css =>builder=>
            builder.CreateElement(100, "style", Controller.StateStyle.TransitionCssClass);

    protected override void OnInitialized()
    {
        Controller.OnUpdating += () => NotifyStateChanged();
        Controller.Init();
        this.DebugLine(nameof(OnInitialized), "初始化");
    }

    protected override void BuildRenderTree(RenderTreeBuilder builder)
    {
            builder.AddContent(0, Css);
            base.BuildRenderTree(builder);

        //builder.OpenElement(1, "div");
        //AddClassAttribute(builder, 2);
        //AddStyleAttribute(builder, 3);
        //builder.AddContent(4, (RenderFragment)(content =>
        //{
        //    content.OpenComponent<Toaster>(0);
        //    content.AddAttribute(1,nameof(Toaster.Setting),Controller.Setting);
        //    content.CloseComponent();
        //}));
        //builder.CloseElement();

    }

    protected override void BuildCssClass(ICssClassBuilder builder)
    {
        builder.Append("floating")
        .Append("toast-box")
        .Append("compact")
        .Append("unclickable")
        .Append("animating")
        .Append("transition")
        .Append("visible")
        ;
    }

    protected override void BuildStyle(IStyleBuilder builder)
    {
        builder.Append(Controller.StateStyle.AnimationStyle);
    }

    protected override void BuildComponentRenderTree(RenderTreeBuilder builder)
    {
        base.BuildComponentAttributes(builder, out int sequence);

        //if (Setting.ProgressBar.HasValue && Setting.ProgressBar.Value == VerticalPosition.Top)
        //{
        //    BuildProgress(builder, sequence + 1);
        //}

        builder.CreateComponent<Toaster>(sequence + 2, attributes: new { Controller.Setting });

        //if (Setting.ProgressBar.HasValue && Setting.ProgressBar.Value == VerticalPosition.Bottom)
        //{
        //    BuildProgress(builder, sequence + 3);
        //}
    }

    ///// <summary>
    ///// Builds the progress.
    ///// </summary>
    ///// <param name="builder">The builder.</param>
    //private void BuildProgress(RenderTreeBuilder builder, int sequence)
    //{
    //    builder.CreateComponent<Progress>(sequence, attributes: new
    //    {
    //        Attached = true,
    //        AttachedVertical = Setting.ProgressBar,
    //        Percent = _progress,
    //        Color = Setting.ProgressBarColor ?? Setting.Color,
    //        Setting.Inverted,
    //        State = Setting.ProgressBarState ?? Setting.State
    //    });
    //}
    ///// <summary>
    ///// Calculates the progress.
    ///// </summary>
    ///// <param name="percentComplete">The percent complete.</param>
    //private async void CalculateProgress(int percentComplete)
    //{
    //    _progress = Setting.ProgressBarIncrease == HorizontalPosition.Left ? percentComplete : 100 - percentComplete;
    //    await InvokeAsync(StateHasChanged);
    //}


    public void Dispose()
    {
        Controller.OnUpdating -= () => NotifyStateChanged();
    }
}
